package com.mandala.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mandala.LDAPConnectService;
import com.mandala.entry.ApplicationSystem;
import com.mandala.entry.ResponseWrapper;
import com.mandala.service.JsonResult;
import com.mandala.service.OdmUserRepo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/system")
@Api("业务系统查询接口")
public class SystemController {
	@Autowired
	private OdmUserRepo odmUserRepo;
	@Autowired
	private JsonResult jsonResult;
	@Autowired
	private LDAPConnectService connectService;
	
    @ApiOperation(value = "根据应用系统CN", notes = "系统查询")
    @ApiImplicitParam(name = "cn", value = "应用系统ID", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getSystemByCn/{cn}" , method = RequestMethod.GET)
    public  ResponseWrapper getSystemByCn(@PathVariable("cn") String cn) {
    	ResponseWrapper response = new ResponseWrapper();
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	if(cn.equals("null") || cn ==null){
    		return jsonResult.markParamError();
    	}
    	ApplicationSystem system = new ApplicationSystem();
    	system = odmUserRepo.findApplicationSystemByCn(cn);
		if(system != null){
			response = jsonResult.markSuccess(system);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
    
    /*@ApiOperation(value="获取所有业务系统列表", notes="获取所有业务系统列表")
    @RequestMapping(value={""}, method=RequestMethod.GET)
    public List<ApplicationSystem> getApplicationSystemList() {
    	List<ApplicationSystem> r = odmUserRepo.findAllApplicationSystem();
          return r;
     }*/
    
    @ApiOperation(value="根据用户id", notes="用户系统查询")
    @ApiImplicitParam(name = "member", value = "用户id", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value={"/getSystemByUserId/{member}"}, method=RequestMethod.GET)
    public ResponseWrapper getAllSystemByUserId(@PathVariable("member") String member) {
    	ResponseWrapper response = new ResponseWrapper();
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	if(member.equals("null") || member ==null){
    		return jsonResult.markParamError();
    	}
    	List<ApplicationSystem> list = odmUserRepo.findAllSystemByUserId(member);
    	if(list.size()>0){
			response = jsonResult.markSuccess(list);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
     }
    
    
}